﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace OpSatPlan
{
    public class Identifier
    {
        private AtomicFormula mFormula;
        private bool mValue;
        private IdentifierTypes mType;
        private ReadOnlyCollection<Identifier> mSubIdentifiers;
        private int? mHashCode;
        public Identifier(AtomicFormula formula, bool value, IdentifierTypes type, IList<Identifier> subList)
        {
            this.mFormula = formula;
            this.mValue = value;
            this.mType = type;
            this.mSubIdentifiers = new ReadOnlyCollection<Identifier>(subList);
            this.mHashCode = null;
        }
        public AtomicFormula AtomicFormula
        {
            get
            {
                return this.mFormula;
            }
        }
        public bool Value
        {
            get
            {
                return this.mValue;
            }
        }
        public IdentifierTypes IdentifierType
        {
            get
            {
                return this.mType;
            }
        }
        public ReadOnlyCollection<Identifier> SubIdentifiers
        {
            get
            {
                return this.mSubIdentifiers;
            }
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            else if (obj.GetType() == typeof(Identifier))
                return this == (Identifier)obj;
            else
                return false;
        }

        public static bool operator ==(Identifier x, Identifier y)
        {
            if (System.Object.ReferenceEquals(x, y))
                return true;
            else if ((object)x == null || (object)y == null)
                return false;
            else
                return x.GetHashCode() == y.GetHashCode();

        }

        public static bool operator !=(Identifier x, Identifier y)
        {
            return !(x == y);
        }

        public override int GetHashCode()
        {
            if (this.mHashCode == null)
            {
                int result = 17;
                result = result * 37 + this.mFormula.GetHashCode();
                result = result * 37 + this.mValue.GetHashCode();
                result = result * 37 + this.mType.GetHashCode();
                for (int i = 0; i < this.mSubIdentifiers.Count; i++)
                    result = result * 37 + this.mSubIdentifiers[i].GetHashCode();
                this.mHashCode = result;
            }
            return this.mHashCode.Value;
        }
    }
}
